package com.notme.widget.recycleview.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.support.v4.view.ViewCompat
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.View
import android.view.ViewGroup

class LineItemDecoration(val context: Context, private val oritation: Int) : RecyclerView.ItemDecoration() {
    var dividerDrawable: Drawable?

    init {
        val typedArray = context.obtainStyledAttributes(arrayOf(android.R.attr.listDivider).toIntArray())
        dividerDrawable = typedArray.getDrawable(0)
        Log.i(
            "ItemDecoration",
            "dividerDrawable:width:${dividerDrawable?.intrinsicWidth},height:${dividerDrawable?.intrinsicHeight}"
        )
        typedArray.recycle()
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        dividerDrawable?.let {
            if (oritation == LinearLayoutManager.VERTICAL) {
                drawVertical(c, parent)
            } else {
                drawHorizontal(c, parent)
            }
        }
    }

    private fun drawHorizontal(c: Canvas, parent: RecyclerView) {
        dividerDrawable?.let {
            val top = parent.paddingTop
            val bottom = parent.height - parent.paddingBottom
            for (i in 0 until parent.childCount) {
                val child = parent.getChildAt(i)
                val layoutParams = child.layoutParams as ViewGroup.MarginLayoutParams
                val left = child.right + layoutParams.rightMargin + ViewCompat.getTranslationX(child)
                val right = left + it.intrinsicWidth
                it.setBounds(left.toInt(), top, right.toInt(), bottom)
                Log.i(
                    "ItemDecoration",
                    "dividerDrawable:left:$left,top:$top,right:$right,bottom:$bottom"
                )
                it.draw(c)
            }
        }
    }

    private fun drawVertical(c: Canvas, parent: RecyclerView) {
        dividerDrawable?.let {
            val left = parent.paddingLeft
            val right = parent.width - parent.paddingRight
            for (i in 0 until parent.childCount) {
                val child = parent.getChildAt(i)
                val layoutParams = child.layoutParams as ViewGroup.MarginLayoutParams
                val top = child.bottom + layoutParams.bottomMargin + ViewCompat.getTranslationY(child)
                val bottom = top + it.intrinsicHeight
                it.setBounds(left, top.toInt(), right, bottom.toInt())
                Log.i(
                    "ItemDecoration",
                    "dividerDrawable:left:$left,top:$top,right:$right,bottom:$bottom"
                )
                it.draw(c)
            }
        }
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)
        dividerDrawable?.let {
            if (oritation == LinearLayoutManager.VERTICAL) {
                outRect.set(0, 0, 0, it.intrinsicHeight)
            } else {
                outRect.set(0, 0, it.intrinsicWidth, 0)
            }
        }
    }
}